package org.ala.tiktools.sharddb.db.provider.druid;

import com.alibaba.druid.filter.FilterChainImpl;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.SQLException;

/**
 * @author ala
 * @date 2025-03-25 11:32
 */
public class DruidDataSourceExpander extends DruidDataSource {
    private static final long serialVersionUID = 457058712862615350L;

	/**
     * 结论：在高并发场景下，Druid的性能并不高
     * <p>  50线程，拿链接的延迟可以达到5s！！！
     */
    public DruidPooledConnection getConnection(long maxWaitMillis) throws SQLException {
//        long now = System.currentTimeMillis();
        init();
//        System.out.printf("[DruidDataSource getConnection] 初始化耗时 thread:%s delay:%d\n", Thread.currentThread().getName(), System.currentTimeMillis() - now);
//        now = System.currentTimeMillis();

        if (!filters.isEmpty()) {
            FilterChainImpl filterChain = new FilterChainImpl(this);
            return filterChain.dataSource_connect(this, maxWaitMillis);
        } else {
            DruidPooledConnection conn = getConnectionDirect(maxWaitMillis);
//            System.out.printf("[DruidDataSource getConnection] 拿链接耗时 thread:%s delay:%d\n", Thread.currentThread().getName(), System.currentTimeMillis() - now);
            return conn;
        }
    }
}
